<?php
/**********************基础数据类型 功能性工具******************************/
/**
 * @Desc     字符串formatter
 * @Author   pekey
 * @DateTime 2020-05-12T19:27:03+0800
 * @return   [type]
 * ex:
 *     $v = str_format("aaa{0}...{1}",$a,$b);
 */
function str_format()
{
    $args = func_get_args();
    if (count($args) == 0) {return;}
    if (count($args) == 1) {return $args[0];}
    $str = array_shift($args);
    $match=function ($match) use ($args){
        return isset($args[$match[1]]) ? $args[$match[1]] : $match[0];
    };
    $str = preg_replace_callback('/\\{(0|[1-9]\\d*)\\}/',$match, $str);
    return $str;
}

/**
 * [checkObj description]
 * @Description 如果前两个参数相同，则变量使用默认值
 * @Author      FengPQ
 * @DateTime    2020-05-20T01:58:04+0800
 * @param       [type]                   $v             [description]
 * @param       [type]                   $check         [description]
 * @param       [type]                   $default_value [description]
 * @return      [type]                                  [description]
 */
function checkObj($v, $check, $default_value)
{
    return $v == $check ? $v : $default_value;
}

/**
 * [NullDeault description]
 * @Description 如果$v为不存在或为空，则显示默认值
 * @Author      FengPQ
 * @DateTime    2020-05-20T09:27:01+0800
 * @param       [type]                   $v             [description]
 * @param       [type]                   $default_value [description]
 */
function NullDeault($v, $default_value = "")
{
    if (isset($v)) {
        return $v;
    }
    return $default_value;
}

/**
 * [str_format description]
 * @Description 接口方法判断参数是否缺少
 * @Author      FengPQ
 * @DateTime    2020-05-20T09:31:17+0800
 * @return      [type]                   [description]
 */
function api_params_check()
{
    $args = func_get_args();
    $isFlag = true;
    if (func_num_args() == 0) {return $isFlag;}
    foreach ($args as $key => $val) {
        if (!isset($val) || empty($val)) {
            $isFlag = false;
            break;
        }
    }
    if (!$isFlag) {
        throw new \TPException(ErrCode::ErrCode_10002);
    }
    return true;
}

/*生成唯一标志
 *标准的UUID格式为：xxxxxxxx-xxxx-xxxx-xxxxxx-xxxxxxxxxx(8-4-4-4-12)
 */

function getUuid()
{
    $chars = md5(uniqid(mt_rand(), true));
    $uuid = substr($chars, 0, 8) . '-'
    . substr($chars, 8, 4) . '-'
    . substr($chars, 12, 4) . '-'
    . substr($chars, 16, 4) . '-'
    . substr($chars, 20, 12);
    return $uuid;
}


/**
 * [copyArray 复制某几项数组元素]
 * @Description 复制某几项数组元素
 * @Author      LiAnXu
 * @Date    2020-12-09 16:38:28 星期三
 * @return      [type]                   [description]
 */
function copyArray($orgi,$items = array()){
	$ret = array();
	foreach($items as $i => $v){
		if(isset($orgi[$v])){
            $ret[$v] = $orgi[$v];
        }
	}
	return $ret;
}


/**
 * @Name 获取方法列和角色方法列
 * @Desc 过滤为空数组
 * @Author      LiAnXu
 * @Date 2020-12-10 14:20:11 星期四
 * @return      [array]                   [数组]
 */
 function filterNull($param)
 {
    $data=array();
    if(is_array($param)){
        foreach ($param as $k => $v) {
            if(!isEmpty($v)){
                $data[$k]=$v;
            }
        }
    }
    return $data;
 }

 /**
 * @Desc     用于判断为空为空字符串判断
 * @Author   pekey
 * @DateTime 2020-05-12T19:25:18+0800
 * @param    [type] 对象参数
 * @return   boolean
 */
function isEmpty($s)
{
    if ($s == null || $s == "") {
        return true;
    }

    return false;
}

 /**
 * @Desc     用于判断为空为空字符串判断
 * @Author   pekey
 * @DateTime 2020-12-14
 * @param    [type] 时间戳
 * @return   boolean
 */
function toTime($time)
{
    $time=date('Y-m-d H:i:s',$time);
    return $time;
}



// 是否有效手机
function validMobile($mobile) {
    return preg_match('/^1[345789]\d{9}$/ims', $mobile) ? true : false;
}


function  filtrfunction($arr) {
    if ($arr === '' || $arr === null) {
        return false;
    }
    return true;
}

function ch2int($ch) {
    $res = 0;

    $char = str_split($ch);
    $len = count($char);

    for ($i = 0; $i < $len; $i++) {
        $res *= 26;
        $res += ord($char[$i]) - ord('A') + 1;
    }
    return $res;
}



function chrnum($chr) {
    if ($chr > 26) {
        $num  = intval(floor($chr / 26));
        $res = $chr % 26;

        if ($res == 0) {
            $num  = $num - 1;
            $res = 26;
        }

        $B = chr((int)$res + 64);
        $A = chr((int)$num + 64);
        return $A . $B;
    } else {
        return chr((int)$chr + 64);
    }
}



